/**
  * @File    :   sx1278_app.c
  * @Time    :   2019/10/27 21:22:54
  * @Author  :   huenrong
  */

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include "./sx1276_app.h"
#include "../gpio/gpio.h"
#include "../sx1276/sx1276.h"

/*!
 * Radio hardware and global parameters
 */
extern SX1276_t SX1276;

/*!
 * \brief Reads the contents of the SX1276 FIFO
 *
 * \param [OUT] buffer Buffer where to copy the FIFO read data.
 * \param [IN] size Number of bytes to be read from the FIFO
 */
extern void SX1276ReadFifo(uint8_t *buffer, uint8_t size);

/**
  * @name   sx1276_init
  * @brief  sx1276_init模块初始化
  * @param  channel: sx1276_init模块通道
  * @retval 无
  */
void sx1276_init(const uint8_t channel)
{
    SX1276Init();

    // SX1276SetChannel(470000000);
    SX1276SetChannelNew(channel);

    SX1276SetTxConfig(MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
                      LORA_SPREADING_FACTOR, LORA_CODINGRATE,
                      LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
                      true, 0, 0, LORA_IQ_INVERSION_ON, 3000);

    SX1276SetRxConfig(MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
                      LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
                      LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
                      0, true, 0, 0, LORA_IQ_INVERSION_ON, true);

    SX1276SetRx(); // 初始化设为接收模式
}

/**
  * @name   sx1276_read_version
  * @brief  sx1276读取版本
  * @param  version: 读取到的版本
  * @retval 无
  */
void sx1276_read_version(uint8_t *version)
{
    SX1276ReadBuffer(0x42, version, 1);
}
